package net.begincode.blog.service.impl;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import net.begincode.blog.bean.vo.UserHonorInfo;
import net.begincode.blog.bean.param.UserQueryParam;
import net.begincode.blog.dao.CodeCoinMapper;
import net.begincode.blog.dao.HonorSetMapper;
import net.begincode.blog.dao.biz.BlogBizMapper;
import net.begincode.blog.dao.biz.CodeCoinBizMapper;
import net.begincode.blog.dao.biz.HonorSetBizMapper;
import net.begincode.blog.dao.biz.UserBizMapper;
import net.begincode.blog.entity.Blog;
import net.begincode.blog.entity.CodeCoin;
import net.begincode.blog.entity.HonorSet;
import net.begincode.blog.entity.User;
import net.begincode.blog.enums.BlogFlagEnum;
import net.begincode.blog.enums.UserFlagEnum;
import net.begincode.blog.service.UserService;
import net.begincode.common.CollectionUtils;
import net.begincode.page.PageResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserBizMapper userBizMapper;
    @Resource
    private CodeCoinBizMapper codeCoinBizMapper;
    @Resource
    private HonorSetBizMapper honorSetBizMapper;
    @Resource
    private BlogBizMapper blogBizMapper;

    @Override
    public User queryUserByEmail(String email) {
        Preconditions.checkArgument(StringUtils.isNotEmpty(email), "email not empty");
        return userBizMapper.selectByEmail(email);
    }

    @Override
    public PageResponse<User> selectByParam(UserQueryParam userQueryParam) {
        Preconditions.checkNotNull(userQueryParam, "userQueryParam not null");
        Preconditions.checkArgument(StringUtils.isNotEmpty(userQueryParam.getFlag()), "flag not empty");
        int totalCount = userBizMapper.countParam(userQueryParam);
        if (totalCount == 0) {
            return PageResponse.empty(userQueryParam.getPageNo(), userQueryParam.getPageSize());
        }
        List<User> users = userBizMapper.selectByParam(userQueryParam, userQueryParam.getOffset(), userQueryParam.getLimit());
        return PageResponse.success(users, userQueryParam.getPageNo(), userQueryParam.getPageSize(), totalCount);
    }

    @Override
    public int updateUserFlag(Long userId, String flag) {
        Preconditions.checkNotNull(userId, "userId not null");
        Preconditions.checkArgument(StringUtils.isNotEmpty(flag),"flag not null");
        Preconditions.checkArgument(UserFlagEnum.checkIn(flag),"flag not recognition");
        User user = new User();
        user.setUserId(userId);
        user.setFlag(flag);
        return userBizMapper.updateSelectiveByUserId(user);
    }

    @Override
    public UserHonorInfo queryUserHonor(Long userId) {

        UserHonorInfo userHonorInfo = new UserHonorInfo();
        User user = userBizMapper.selectByPrimaryKey(userId);
        if(Objects.isNull(user)){
            return userHonorInfo;
        }
        userHonorInfo.setUserName(user.getNickName());
        CodeCoin codeCoin = codeCoinBizMapper.selectByUserId(userId);
        if(Objects.isNull(codeCoin)){
            return userHonorInfo;
        }

        userHonorInfo.setCoinCount(codeCoin.getCount());

        HonorSet honorSet = honorSetBizMapper.selectByCoin(codeCoin.getCount());
        userHonorInfo.setHonorName(honorSet.getHonorTitle());
        return userHonorInfo;
    }

    @Override
    public List<User> queryActiveUser(Integer limit) {
        Set<Long> userSet =  CollectionUtils.safes(blogBizMapper.selectLastBlog(BlogFlagEnum.PUBLIC.getFlagCode(),limit*10))
                .stream().map(Blog::getUserId).collect(Collectors.toSet());
        if(org.springframework.util.CollectionUtils.isEmpty(userSet)){
            return Lists.newArrayList();
        }
        List<User> users =  userBizMapper.selectUserByIds(Lists.newArrayList(userSet));
        if(!org.springframework.util.CollectionUtils.isEmpty(users) && users.size()>limit){
            return users.subList(0,limit);
        }
        return users;
    }
}
